package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 东北网
 * @author Administrator
 * abc
 */
public class DbwNewsAnalyse implements AnalyseNews{
	String[] regex={
			"http://.*.dbw.cn/system/[0-9]+/[0-9]+/[0-9]+/[0-9]{9}.shtml",
			"http://[a-z]*.dbw.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]{9}.shtml",
			"http://.*.dbw.cn/baoliao/WebPortal/lby.aspx\\?id=[0-9].*&type=[0-9].*"
			};
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		if(url.startsWith("http://forum.dbw.cn/")){
			return bRet;
		}
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		String dateStr=null;
		Document doc=Jsoup.parse(htmltxt);
		doc.select("#news_more_page_div_id").remove();
		if(StringUtils.startsWithAny(url,"http://finance.dbw.cn/system")){
			try {
				title=doc.select("h2.f24").text();
				dateStr=doc.select("span.hui").text();
				content =HtmlCleaner.getContentHtml(url, doc.select("table.end-wzi"));
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
				System.err.println(url);
			}
		}else if(StringUtils.startsWithAny(url,"http://enterprise.dbw.cn/system")){
				title=doc.select("td.h font b").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("div.xilan_biaoti").text();
				}
				dateStr=doc.select("div.xilan_shijian").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.xilan_neirong"));
				date=DateUtils.matchDate(dateStr);
		}else if(StringUtils.startsWithAny(url,"http://it.dbw.cn/system/")){
			try {
				Elements temp=doc.select("table[width=97%]");
				title=temp.select("tr").get(1).text();
				dateStr=temp.select("tr").get(2).text();
				content=HtmlCleaner.getContentHtml(url,temp.select("td[align=left]"));
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
				System.err.println(url);
			}
		}else if(StringUtils.startsWithAny(url,"http://comment.dbw.cn/system/")){
				Elements temp=doc.select("div#nextgrayborderbox");
				title=temp.select("div.toptit").text();
				dateStr=temp.select("div.box_gray").text();
				content=HtmlCleaner.getContentHtml(url,temp.select("div.font_gray14px"));
				date=DateUtils.matchDate(dateStr);
		}else if(StringUtils.startsWithAny(url,"http://legal.dbw.cn/system")){
			try {
				Elements temp=doc.select("table[width=98%]");
				title=temp.select("tr").get(0).text();
				if(title == null){
				}
				dateStr=temp.select("tr").get(1).text();
				content=HtmlCleaner.getContentHtml(url,temp.select("table[cellpadding=0] font.black14"));
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
				System.err.println(url);
			}
		}else if(StringUtils.startsWithAny(url,"http://international.dbw.cn/system")){
				title=doc.select("title").text();
				if (title!=null)title=StringUtils.substringBefore(title, "-");
				date=DateUtils.matchDate(doc.select("td.lan").text());
				if(date ==null){
					date=DateUtils.matchDate(url);
				}
				content=HtmlCleaner.getContentHtml(url,doc.select("td.hui p"));
		}else if(StringUtils.startsWithAny(url,"http://society.dbw.cn/system")){
			try {
				Elements temp=doc.select("table[width=98%]");
				title=doc.select("div.tem_bt").text();
				if(title == null){
					title=temp.select("tr").get(0).text();
				}

				dateStr=doc.select("div.gray").text();
				if(dateStr.trim().length()<=0){
					dateStr=temp.select("tr").get(1).text();
				}
				 Elements select=doc.select("div.f14 p");
				content=HtmlCleaner.getContentHtml(url,select);
				if(content.trim().length()<=0){
					content=HtmlCleaner.getContentHtml(url,temp.select("tr").get(4));
				}
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
				System.err.println(url);
			}
		}else if(StringUtils.startsWithAny(url,"http://internal.dbw.cn/system")){
			try {
				Elements titleList = doc.select("td.txtc h1");
				if(titleList.size() > 0){
					title = titleList.get(0).text();
				}
				Element temp=doc.select("table.border").get(2);
				if(StringUtils.isBlank(title)){
					title=temp.select("tr").first().text();
				}
				dateStr=doc.select("td.txtc.gray").text();
				content =HtmlCleaner.getContentHtml(url, temp.select("td.con"));
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
				System.err.println(url);
			}
		}else if(StringUtils.startsWithAny(url,"http://heilongjiang.dbw.cn/")){
			try {
				title=doc.select("div.padding5").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("h1").text();
				}
				dateStr=doc.select("div.blank10px").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.padding20"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.duanluo"));
				}
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
				System.err.println(url);
			}
		}else if(url.startsWith("http://sports.dbw.cn")){
			title=doc.select("div.xilanbaiti").text();
			date=DateUtils.matchDate(doc.select("div.xilantop").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div.xilan_txt"));
		}else if(url.startsWith("http://health.dbw.cn")){
			title=doc.select("h2.f24").text();
			date=DateUtils.matchDate(doc.select("span.hui").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("table.end-wzi"));
		}else if(url.startsWith("http://ms.dbw.cn")){
			title=doc.select("td.title").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("span.title").text();
			}
			date=DateUtils.matchDate(doc.select("td[width=85%]").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div.xialn_neirongl").text());
			}
			content=HtmlCleaner.getContentHtml(url,doc.select("td.black_14"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.wenzhang"));
			}
		}else if(url.startsWith("http://commerce.dbw.cn")){
			title=doc.select("td[height=50] strong").text();
			date=DateUtils.matchDate(doc.select("td[height=34]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.xian"));
		}else if(url.startsWith("http://jixi.dbw.cn")){
			title=doc.select("div.STYLE20").text();
			date=DateUtils.matchDate(doc.select("span.style11").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("span.STYLE39"));
		}else if(url.startsWith("http://tour.dbw.cn")){
			title=doc.select("td.title").text();
			date=DateUtils.matchDate(doc.select("td.blue12").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.black14"));
		}else if(url.startsWith("http://auto.dbw.cn")){
			title=doc.select("h1").text();
			date=DateUtils.matchDate(doc.select("td.blue12").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.hui1422"));
		}else if(url.startsWith("http://songbei.dbw.cn")){
			title=doc.select("h1").text();
			date=DateUtils.matchDate(doc.select("div.xilan_title1").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div.pp"));
		}else if(url.startsWith("http://palj.dbw.cn")){
			title=doc.select("div.title").text();
			date=DateUtils.matchDate(doc.select("div.t02").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("#Zoom"));
		}else if(url.startsWith("http://house.dbw.cn")){
			title=doc.select("div.title").text();
			date=DateUtils.matchDate(doc.select("div.t02").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("#Zoom"));
		}else if(url.startsWith("http://bdh.dbw.cn")){
			title=doc.select("h3").text();
			date=DateUtils.matchDate(doc.select("h5.txt_left_bottom_h5").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div.xilan_main_txt"));
		}else if(url.startsWith("http://entertainment.dbw.cn")){
			title=doc.select("td[height=50]").text();
			date=DateUtils.matchDate(doc.select("td.be0002").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.black14"));
		}else if(url.startsWith("http://cul.dbw.cn")){
			title=doc.select("td[style=font-size:24px; color:#a45d00]").text();
			date=DateUtils.matchDate(doc.select("td[bgcolor=#F9F9F9]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("td.xxx"));
		}else if(url.startsWith("http://life.dbw.cn")){
			title=doc.select("font[size=5]").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.temconnentbox"));
		}else if(url.startsWith("http://hegangnews.dbw.cn")){
			title=doc.select("h1").text();
			date=DateUtils.matchDate(doc.select(".tem_wwwbox").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div.news"));
		}else if(url.startsWith("http://qiqihar.dbw.cn")){
			title=doc.select("div[align=center] font[size=4] b").text();
			date=DateUtils.matchDate(doc.select("div[align=center]").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("span.STYLE28"));
		}else if(url.startsWith("http://appnet.dbw.cn/baoliao/")){
			title=doc.select("div.cen_tit").text();
			date=DateUtils.matchDate(doc.select("div.cen_t_f").text());
			content=HtmlCleaner.getContentHtml(url,doc.select("div.cen_content"));
		}else{
			title=doc.select("h1").text();
			date=DateUtils.matchDate(doc.select("div.temarea_linebox").text());
			content =HtmlCleaner.getContentHtml(url, doc.select("div.temarea_con"));
		}

		if (date==null){
			date=DateUtils.matchDate(url);
		}

		author=JSoupUtils.matchAuthor(doc, "来源：");

		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			if(title.indexOf("-")!=-1){
				title=StringUtils.substringBefore(title,"-");
			}
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url, doc.select(".fontblue14"));
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(StringUtils.trimSpace(content));
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}
	public static void main(String[] args) {
		DbwNewsAnalyse a=new DbwNewsAnalyse();
		String url="http://internal.dbw.cn/system/2015/12/07/056977857.shtml";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);

		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
